package nba2kmodtool.filedesc.v2012;

import java.io.IOException;
import java.nio.ByteBuffer;

import nba2kmodtool.filedesc.BaseIffFile;
import nba2kmodtool.filedesc.N2kModelPartReader;
import nba2kmodtool.util.BufferUtil;

class N2k12ModelPartReader extends N2kModelPartReader {

	protected N2k12ModelPartReader(BaseIffFile iff) {
		super(iff);
	}

	@Override
	protected float[] readVertexCoordinate(ByteBuffer buffer, int perVertexSize, boolean isMod) {
		float x = 0, y = 0, z = 0;
		if (isMod) {
			x = BufferUtil.readBytesAsShort(buffer, 2);
			y = BufferUtil.readBytesAsShort(buffer, 2);
			z = BufferUtil.readBytesAsShort(buffer, 2);
			x /= MOD;
			y /= MOD;
			z /= MOD;
		} else {
			switch (perVertexSize) {
			case 4:
				x = buffer.get();
				y = buffer.get();
				z = buffer.get();
				x /= BYTE_MOD;
				y /= BYTE_MOD;
				z /= BYTE_MOD;
				break;
			default:
				x = BufferUtil.readFloat(buffer);
				y = BufferUtil.readFloat(buffer);
				z = BufferUtil.readFloat(buffer);
			}
		}
		//System.out.println(x + "," + y + "," + z);
		return new float[] { x, y, z };
	}

	@Override
	protected float[] readMultiChunkVertexUVCoordinate(ByteBuffer buffer, int perVertexSize, int perUVNSize,
			boolean isMod) throws IOException {
		float u = 0, v = 0;
		switch (perUVNSize) {
		case 40:
			buffer.position(buffer.position() + 0x18);
			u = BufferUtil.readFloat(buffer);
			v = BufferUtil.readFloat(buffer);
			break;
		case 36:
			buffer.position(buffer.position() + 0x18);
			break;
		case 28:
			buffer.position(buffer.position() + 0x10);
			break;
		case 24:
			break;
		case 16:
			buffer.position(buffer.position() + 0x04);
		case 4:
			break;
		default:
			throw new IOException("Unkown UVN data Type! " + perUVNSize);
		}
		u = BufferUtil.readBytesAsShort(buffer, 2);
		v = BufferUtil.readBytesAsShort(buffer, 2);
		u /= MOD;
		v = 1F - (v / MOD);
		//System.out.println(u + ", " + v);
		return new float[] { u, v };
	}

	@Override
	protected float[] readVertexUVCoordinate(ByteBuffer buffer, int perVertexSize, boolean isMod) throws IOException {
		float u = 0, v = 0;
		switch (perVertexSize) {
		case 56:
			buffer.position(buffer.position() + 0x4);
			break;
		case 52:
			buffer.position(buffer.position() + 0x18);
			u = BufferUtil.readFloat(buffer);
			v = BufferUtil.readFloat(buffer);
			break;
		case 48:
			buffer.position(buffer.position() + 0x18);
			u = BufferUtil.readBytesAsInt(buffer, 2);
			v = BufferUtil.readBytesAsInt(buffer, 2);
			u /= MOD;
			v = 1F - (v / MOD);
			break;
		case 42:
			buffer.position(buffer.position() + 0x04);
			break;
		case 36:
			if (isMod) {
				buffer.position(buffer.position() + 18);
				u = BufferUtil.readBytesAsInt(buffer, 2);
				v = BufferUtil.readBytesAsInt(buffer, 2);
				u /= MOD;
				v = 1F - (v / MOD);
			} else {
				buffer.position(buffer.position() + 8);
				u = BufferUtil.readFloat(buffer);
				v = BufferUtil.readFloat(buffer);
			}
			break;
		case 28:
			u = BufferUtil.readBytesAsInt(buffer, 2);
			v = BufferUtil.readBytesAsInt(buffer, 2);
			u /= MOD;
			v = 1F - (v / MOD);
			break;
		case 4:
			break;
		default:
			throw new IOException("Unkown Vertex data Type! " + perVertexSize);
		}
		return new float[] { u, v };
	}

}
